package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.demo.Service.CategoryService;
import com.waimai.demo.Service.Serviceimpl.CategoryServiceImpl;
import com.waimai.demo.Service.SetmealService;
import com.waimai.demo.back.R;
import com.waimai.demo.back.runtimeException;
import com.waimai.demo.domain.Category;
import com.waimai.demo.domain.Employee;
import com.waimai.demo.domain.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    CategoryServiceImpl categoryService;
    @Autowired
    SetmealService setmealService;

    @PostMapping
    public R<String> sava(@RequestBody Category category, HttpServletRequest req) {
        Category category1 = categoryService.getById(category.getId());
        if (category1 == null) {
            log.info("一计害三贤");
            category.setCreateTime(LocalDateTime.now());
            category.setUpdateTime(LocalDateTime.now());
            Long id = (Long) req.getSession().getAttribute("employee");
            category.setCreateUser(id);
            category.setUpdateUser(id);
            categoryService.save(category);

            return R.success("新增分类成功");
        }
        throw new runtimeException("这个已经重复了哦，不能增加了，老铁");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lmq = new LambdaQueryWrapper();

        lmq.orderByAsc(Category::getSort);
        categoryService.page(page1, lmq);
        return R.success(page1);
    }

    @DeleteMapping
    public R<String> remove(Long ids) {
        log.info("他的id是" + ids);
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updeta(@RequestBody Category category, HttpServletRequest req) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Long id = (Long) req.getSession().getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List> savaCategory(Category category) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        List<Category> categories = categoryService.list(lambdaQueryWrapper);
        return R.success(categories);
    }


}
